package com.nf.action;

import com.nf.entity.User;
import com.nf.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class UserAction extends ActionSupport {

    private Map<String,Object> jsonMap;
    public Map<String, Object> getJsonMap() {
        return jsonMap;
    }
    public void setJsonMap(Map<String, Object> jsonMap) {
        this.jsonMap = jsonMap;
    }

    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Autowired
    private UserService userService;

    public String showAllUser(){
        List<User> userList = userService.getAllUser();
        //把结果传递给前端jsp页面
        ActionContext.getContext().getValueStack().set("userList",userList);
        return this.SUCCESS;
    }

    public String isWin(){
        System.out.println("前端传过来的id:"+id);
        boolean flag = userService.isWin(id);
        System.out.println("结果："+flag);
        jsonMap = new HashMap<String, Object>();
        jsonMap.put("status",1);//代表没有异常，成功生成数据
        if (flag==true){
            jsonMap.put("message","恭喜!已经获奖了（给前端工程师看）");
            jsonMap.put("data",true);
        }else {
            jsonMap.put("message","恭喜!你输了（给前端工程师看）");
            jsonMap.put("data",false);
        }
        return "jsonOK";
    }
}
